package patrol.mobile.com.mobilepatrol.activity.security.emergency;


import android.content.Intent;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.bean.ProtectListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;
/**
 * 综合查询模块
 */
public class ZHCXEmergencyProtectListActivity extends CommonListActivity implements CallBack<DateBean> {
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();
    private View itemTopView;
    private MySpinner spOrgId;
    private MySpinner spStartTime;
    private MySpinner spEndTime;

    private int clickFlag;//点击选择时间的标志 1是开始时间 2是结束时间
    private String currentOrgId = "";
    private String currentStartTime = "";
    private String currentEndTime = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private int page = 1;
    private List<ProtectListBean> protectListBeanList = new ArrayList<>();

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new patrol.mobile.com.mobilepatrol.activity.security.adapter.ZHCXEmergencyProtectListAdapter(this);
    }

    @Override
    public void initView() {
        super.initView();
        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_protect_list_top, null);
        spOrgId = itemTopView.findViewById(R.id.spOrgId);
        spStartTime = itemTopView.findViewById(R.id.spStartTime);
        spEndTime = itemTopView.findViewById(R.id.spEndTime);
        flContent.addView(itemTopView);
        spOrgId.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        spStartTime.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        spEndTime.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_PROTECT_LIST;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_right_title.setVisibility(View.GONE);
        //iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageDrawable(getResources().getDrawable(R.mipmap.sys));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getOrg();
    }

    @Override
    public void setListener() {
        super.setListener();
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(EmergencyProtectListActivity.this, EmergencyPackageSignInActivity.class);
//                intent.putExtra("sigin", (ArrayList) protectListBeanList);
//                startActivity(intent);


                Intent intent = new Intent(ZHCXEmergencyProtectListActivity.this, EmergencySignScanActivity.class);
                intent.putExtra("sigin", (ArrayList) protectListBeanList);
                startActivity(intent);
            }
        });

        //currentOrgId = TextUtils.isEmpty(UserManager.getOrgNo()) ? "" : UserManager.getOrgNo();
        //spOrgId.getmText().setText(TextUtils.isEmpty(UserManager.getUser().bmname) ? "" : UserManager.getUser().bmname);
//        spOrgId.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                // DatePickerUtil.getInstance(EmergencyProtectListActivity.this).setCallBack(EmergencyProtectListActivity.this);
//            }
//        });

        spStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickFlag = 1;
                DatePickerUtil.getInstance(ZHCXEmergencyProtectListActivity.this).setCallBack(ZHCXEmergencyProtectListActivity.this);
            }
        });

        spEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickFlag = 2;
                DatePickerUtil.getInstance(ZHCXEmergencyProtectListActivity.this).setCallBack(ZHCXEmergencyProtectListActivity.this);
            }
        });

        spStartTime.getmText().setText(sdf.format(new Date()));
        spEndTime.getmText().setText(sdf.format(new Date()));

        currentStartTime = spStartTime.getmText().getText().toString();
        currentEndTime = spEndTime.getmText().getText().toString();

        // requestData(currentOrgId, sdf.format(new Date()), sdf.format(new Date()));
    }

    @Override
    public void configAdapter() {
        super.configAdapter();


    }


    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                orgNameList = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }
                spOrgId.getmText().setText(orgListBean.getList().get(0).getOrgName());
                currentOrgId=orgListBean.getList().get(0).getId();
                spOrgId.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        currentOrgId=orgListBean.getList().get(postion).getId();
                        requestData(currentOrgId, currentStartTime, currentEndTime);
//                        String orgNme = spOrgId.getmText().getText().toString();
//                        for (int i = 0; i < orgNameList.size(); i++) {
//                            if (orgNme.equals(orgNameList.get(i))) {
//                                currentOrgId = orgIdList.get(i);
//                                requestData(currentOrgId, currentStartTime, currentEndTime);
//                                return;
//                            }
//                        }


                    }
                });

                String orgNme = spOrgId.getmText().getText().toString();
                for (int i = 0; i < orgNameList.size(); i++) {
                    if (orgNme.equals(orgNameList.get(i))) {
                        currentOrgId = orgIdList.get(i);
                        requestData(currentOrgId, currentStartTime, currentEndTime);
                        return;
                    }
                }


            }
        });
    }


    private void requestData(String orgId, String startTime, String endTime) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_PROTECT_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("startDate", startTime);
        map.put("endDate", endTime);
        Log.e("request",new Gson().toJson(map));
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                protectListBeanList = GsonUtil.jsonToList(listArray.toString(), ProtectListBean.class);
                mAdapter.updateItems(protectListBeanList);
            }
        });
    }


    @Override
    public void onBack(DateBean dateBean) {
        if (clickFlag == 1) {
            currentStartTime = dateBean.getmDate();
            spStartTime.getmText().setText(dateBean.getmDate());
        } else {
            currentEndTime = dateBean.getmDate();
            spEndTime.getmText().setText(dateBean.getmDate());
        }
        requestData(currentOrgId, currentStartTime, currentEndTime);
    }
}
